package com.huatai.datacenter.controller;

import com.alibaba.fastjson.JSONObject;
import com.huatai.common.api.R;
import com.huatai.datacenter.entity.DataServicesEntity;
import com.huatai.datacenter.service.ApiService;
import com.huatai.datacenter.service.DataServices;
import com.huatai.datacenter.service.HttpRequestService;
import com.huatai.datacenter.service.TopicTaskService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author Lion
 * @date 2023/6/18  18:58
 */
@RestController
@RequestMapping("/dataServe")
@Api(value = "实时数据服务",tags = "实时数据服务")
public class DataServiceController extends BaseController<DataServices, DataServicesEntity>{

	@Autowired
	private TopicTaskService taskService;

	//添加完数据服务，如果获取到该数据，将数据写入到到数据服务中
	@ApiOperation("新增获取更新")
	@Transactional
	@Override
	public R save(@ApiParam(required = true) @RequestBody DataServicesEntity data) {

		String targetTopic = taskService.getById(data.getTaskId()).getTopicName();
		data.setTargetTopic(targetTopic);

		this.service.SaveOrUpdateWithValid(data);
		this.service.saveServiceApply(data);

		return R.success("保存成功");
	}

	//一个接口返回实时任务数据、非实时任务数据 API接口数据
	/**
	 * @author Lion
	 * @description 首页选项卡数据
	 * @param
	 * @return com.huatai.common.api.R
	 */
	@ApiOperation("首页，获取选项卡数据")
	@PostMapping("/queryHomeCount")
	public R queryHomePageData(){

		List<JSONObject> list = super.service.queryData();

		return R.data(list);

	}

	/**
	 * @author Lion
	 * @description 饼图展示实时接入数据量和非实时接入数据量
	 * @param
	 * @return com.huatai.common.api.R
	 */
	@ApiOperation("获取饼图数据")
	@PostMapping("/queryPieCount")
	public R queryPieData(){

		List<JSONObject> list = super.service.queryPieData();

		return R.data(list);
	}

}
